package pt.spjm.pmartins.lapcounter;

import java.util.ArrayList;
import java.util.HashMap;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;
import android.widget.SimpleAdapter;

public class Setup_Pilots_List extends Activity {

	private ListView lv;
	private Obj_CurrentData cData;
	private Context c;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.list);
				
		lv = (ListView) findViewById(R.id.listView);
		
		cData = LapCounter.cData;
		c = this;
		
		SimpleAdapter sa = new SimpleAdapter(this, loadPilotsList(), R.layout.setup_pilots_list_row, 
				new String[] { "pNumber", "pName" },
				new int[] { R.id.edit_pNumber, R.id.edit_pName });
		lv.setAdapter(sa);
		
		lv.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> arg0, View arg1, final int arg2, long arg3) {

				Intent intent = new Intent();
				intent.setClass(c,  Setup_Pilots_Edit.class);
				intent.putExtra("id", arg2);
				startActivity(intent);	
				finish();
			}
		});
		
		
	}
	
	public ArrayList<HashMap<String, String>> loadPilotsList(){
		ArrayList<HashMap<String, String>> list = new ArrayList<HashMap<String, String>>();
		HashMap<String, String> row;
		
		for(Obj_Pilot p : cData.getPilots()){
			row = new HashMap<String, String>();
			row.put("pNumber", "" + p.getNumber());
			row.put("pName", p.getName());
			list.add(row);
		}
		return list;
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		return true;
	}
}
